<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 权限管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="exportExeclRole"
                >导入
                </el-button>
                <el-button type="primary" icon="el-icon-circle-plus-outline" class="handle-del mr10" @click="addRoles"
                >添加角色
                </el-button>

                <el-input v-model="query.roleName" placeholder="用户名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="roleId" label="角色编号" width="155" align="center"></el-table-column>
                <el-table-column prop="roleName" label="角色名" align="center"></el-table-column>

                <el-table-column prop="introduce" label="角色介绍" align="center"></el-table-column>

                <el-table-column label="操作" width="280" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-s-custom" class="handle-del mr10"
                                   @click="handleRole(scope.$index, scope.row)">
                            授权
                        </el-button>
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑
                        </el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="jumper,total, prev, pager, next, sizes"
                        @size-change="handleSizeChange"
                        :current-page.sync="query.pageIndex"
                        :page-sizes="[5, 10, 20, 30]"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="角色名">
                    <el-input v-model="form.dialogRoleName"></el-input>
                </el-form-item>
                <el-form-item label="角色介绍">
                    <el-input v-model="form.dialogIntroduce"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 授权弹出层 -->
        <el-dialog title="授权" :visible.sync="dialogVisible" width="50%">
                <el-tree
                    :data="data"
                    show-checkbox
                    node-key="id"
                    ref="tree"
                    :default-expanded-keys="ids"
                    :default-checked-keys="ids"
                    :props="defaultProps">
            </el-tree>
            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="getResourceIds">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import {roleData, deleteRole, exitRole, exportExeclRole, getrs, batchAdd} from '../../api/role';
    import {resourceData, treeResourceRole} from '../../api/resource';

    export default {
        name: 'role',
        data() {
            return {
                ids: [],
                query: {
                    roleId: '',
                    roleName: '',
                    introduce: '',
                    pageIndex: 1,
                    pageSize: 5
                },
                multipleSelection: [],
                delList: [],
                tableData: [],
                data: [{
                    id: 1,
                    label: 'label',
                    children: [{
                        id: 4,
                        label: 'label',
                        children: [{
                            id: 9,
                            label: 'label',
                        }]
                    }]
                }],
                defaultProps: {
                    children: 'children',
                    label: 'label'
                },
                dialogVisible: false,
                editVisible: false,
                addValidateEvents: false,
                pageTotal: 0,
                form: {
                    dialogRoleName: "",
                    dialogIntroduce: "",
                    dialogRoleId: 1,
                },
                idx: -1,
                id: -1,
                rid: 1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            getResourceIds() {
                let str = "";
                let checkedKeys = this.$refs.tree.getCheckedKeys();//获取当前vue中的tree中的选中的checkbox
                for (let i = 0; i < checkedKeys.length; i++) {
                    str += checkedKeys[i] + ",";
                }
                batchAdd(this.rid, str).then(res => {
                    this.dialogVisible=false;
                    this.$message.success(res.data);
                })
            },
            // 获取 easy-mock 的模拟数据
            getData() {
                roleData(this.query).then(res => {
                    this.tableData = res.data.data;
                    this.pageTotal = res.data.count
                });

            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            }, rowRole(row) {
                alert(row);
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                })
                    .then(() => {
                        console.log(row.roleId)
                        deleteRole(row.roleId)
                        this.$message.success('删除成功');
                        this.tableData();
                        this.idx = index;
                        this.getData();
                    })
                    .catch(() => {
                    });
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            //导出
            exportExeclRole() {
                exportExeclRole(this.form).then(res => {
                    this.getData();
                });
            },
            addRoles() {
                this.$router.push("/addrole");
                this.getData();
            },
            // 编辑操作
            handleEdit(index, row) {
                this.form.dialogRoleName = row.roleName;
                this.form.dialogIntroduce = row.introduce;
                this.form.dialogRoleId = row.roleId;
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                this.editVisible = false;
                this.$message.success(`修改成功`);
                this.$set(this.tableData, this.dialogResourceName, this.dialogIntroduce);
                exitRole(this.form).then(res => {
                    this.getData();
                });
            },
            //分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
            handleSizeChange(val) {
                this.query.pageSize = val;
                this.getData();
            },
            handleRole(index, row) {
                this.rid = row.roleId;
                this.dialogVisible = true;
                treeResourceRole().then(res => {
                    this.data = res.data;
                });
                getrs(row.roleId).then(res => {
                    this.ids = res.data
                })
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }

    .mask {
        height: 100%;
        position: fixed;
        top: 0;
        left: 0;
        opacity: 0;
        z-index: 11;
    }

    .common-tree {
        overflow: auto;
    }

    .tree-select {
        z-index: 111;
    }

    .ok {
        float: right;
    }

    .el-row {
        padding-top: 0px !important;
    }

</style>
